#include "DdsClientWrapper.hpp"
#include <QDebug>

bool DdsClientWrapper::initialize(uint32_t domainId, const QString& deviceID)
{
    if(subStatu_) {
        qWarning() << "DDS nodes already initialized";
        return false;
    }

    domainId_ = domainId;
    mdeviceID = deviceID;
    pubTopicName = deviceID;
    QString subStatuTopic = deviceID + "/status";
    QString subEventTopic = deviceID + "/event";
    subStatuTopic = deviceID;
    //subEventTopic = deviceID + "02";
    try {
        //client_ = std::make_unique<CNPClient>(domainId_, pubTopicName.toStdString());
        subStatu_ = std::make_unique<CNPClient>(domainId_, subStatuTopic.toStdString());
        //subEvent_ = std::make_unique<CNPClient>(domainId_, subEventTopic.toStdString());
        return true;
    } catch(const std::exception& e) {
        qCritical() << "DDS initialization failed:" << e.what();
        //client_.reset();
        subStatu_.reset();
        //subEvent_.reset();
        return false;
    }
}



CNP_RESULT DdsClientWrapper::publishCommand(const QString &content)
{
    if (!client_) {
        qWarning() << "DDS publisher not initialized";
        return CNP_RESULT::CNP_ERROR;
    }

    CnpMsg msg;
    // 设置消息内容（注意：QString 转 std::string）
    msg.content(content.toStdString());
    CNP_RESULT result = client_->publishCmdRequest(msg);
    if(result == CNP_RESULT::CNP_PUBLISH_SUCCESS)
    {
        qDebug() << "Message" << content << "published successfully.";
    }
    else
    {
        qDebug() << "Failed to publish message:" << content;
    }
    return result;
}

CNP_RESULT DdsClientWrapper::startSubscription()
{
    if (!subStatu_) {
        qWarning() << "Status subStatuScriber not initialized";
        return CNP_RESULT::CNP_ERROR;
    }

    // 使用 lambda 将 subscribeCallback 转为成员函数调用
    auto callback = [this](const CnpMsg &msg) {
        this->interStatuCallback(msg);
    };

    // 这里直接传入 lambda，假设 subscribeCmdRequest 接收 std::function
    CNP_RESULT result = subStatu_->subscribeStatus(callback);

    if (result == CNP_RESULT::CNP_SUBSCRIBE_SUCCESS)
    {
        qDebug() << "Status Subscription started successfully.";
    }
    else
    {
        qDebug() << "Status Subscription failed.";
    }

    //事件回调为subscribeEvent()
    //return result;

    // 使用 lambda 将Callback 转为成员函数调用
    auto callbackEvent = [this](const CnpMsg &msg) {
        this->interEventCallback(msg);
    };

    // 这里直接传入 lambda，假设 subscribeCmdRequest 接收 std::function
    CNP_RESULT resultEvent = subStatu_->subscribeEvent(callbackEvent);
    if (resultEvent == CNP_RESULT::CNP_SUBSCRIBE_SUCCESS)
    {
        qDebug() << "Event Subscription started successfully.";
    }
    else
    {
        qDebug() << "Event Subscription failed.";
    }

    if(resultEvent == CNP_RESULT::CNP_SUBSCRIBE_SUCCESS)
        return result;
    else
        return resultEvent;


}

void DdsClientWrapper::interStatuCallback(const CnpMsg &msg)
{
    QString content = QString::fromStdString(msg.content());
    //qDebug() << "Internal Status received message:" << content;
    // 发出信号，由主线程接收并更新 UI
    emit msgStatuReceived(content);
}

void DdsClientWrapper::interEventCallback(const CnpMsg &msg)
{
    QString content = QString::fromStdString(msg.content());
    //qDebug() << "Internal Event  received message:" << content;
    // 发出信号，由主线程接收并更新 UI
    emit msgEventReceived(content);
}

void DdsClientWrapper::shutdown()
{
    //client_.reset();
    subStatu_.reset();
    qDebug() << "DDS nodes shutdown successfully";
}
